<!DOCTYPE html>
<html lang="en">

<head>
  <meta charset="UTF-8">
  <meta name="viewport" content="width=device-width, initial-scale=1.0">
  <title>Document</title>
</head>

<body>
  <script>
    var x = 1;
    var obj = {
      x: 2
    }

    obj.fn = (function (x) {
      this.x *= x++;
      return function (y) {
        x += y;
        this.x *= ++x;
        console.log(x);
      }
    })(obj.x);
    var fn = obj.fn;
    obj.fn(2);
    fn(1);
    console.log(obj.x, x);

  </script>
</body>

</html>